package com.forum.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.forum.pojo.UserMessage;
import com.forum.pojo.vo.FriendMessageVo;
import com.forum.pojo.vo.LikeArticleVo;
import com.forum.pojo.vo.LikeCommentVo;
import com.forum.result.Result;
import com.forum.service.BlockRelaService;
import com.forum.service.UserMessageService;
import com.forum.utils.JwtHelper;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName UserMessageController
 * @Description TODO
 * @Author suyao
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/userMessage")
public class UserMessageController {
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private BlockRelaService blockRelaService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "获取自己点赞过的帖子")
    @RequestMapping(value = "/likeArticle", method = RequestMethod.GET)
    public Result getLikeArticle(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getLikeArticle(userId));
    }

    @ApiOperation(value = "获取自己点赞过的评论")
    @RequestMapping(value = "/likeComment", method = RequestMethod.GET)
    public Result getLikeComment(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getLikeComment(userId));
    }

    @ApiOperation(value = "获取别人点赞我的文章消息")
    @RequestMapping(value = "/articleLikeMessage", method = RequestMethod.GET)
    public Result getArticleLikeMessage(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getArticleLikeMessage(userId));
    }

    @ApiOperation(value = "获取别人点赞我的评论消息")
    @RequestMapping(value = "/commentLikeMessage", method = RequestMethod.GET)
    public Result getCommentLikeMessage(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getCommentLikeMessage(userId));
    }

    @ApiOperation(value = "获取好友申请")
    @RequestMapping(value = "/friendMessage", method = RequestMethod.GET)
    public Result getFriendMessage(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getFriendMessage(userId));
    }

    @ApiOperation(value = "获取评论通知")
    @RequestMapping(value = "/commentMessage", method = RequestMethod.GET)
    public Result getCommentMessage(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getCommentMessage(userId));
    }

    /**
     * @param request
     * @param friendId 添加好友id
     * @param addType  同意还是拒绝   0|1
     * @return
     */
    @ApiOperation(value = "添加好友处理")
    @RequestMapping(value = "/handleAddFriend", method = RequestMethod.POST)
    public Result HandleAddFriend(HttpServletRequest request, @RequestParam Long friendId, @RequestParam Integer addType) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.HandleAddFriend(userId, friendId, addType));
    }

    @ApiOperation(value = "获取本人好友")
    @RequestMapping(value = "/getMyFriend", method = RequestMethod.GET)
    public Result getMyFriend(HttpServletRequest request) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getMyFriend(userId));
    }

    @ApiOperation("删除好友")
    @RequestMapping(value = "/deleteFriend", method = RequestMethod.POST)
    public Result deleteFriend(HttpServletRequest request, @RequestParam Long friendId) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        if(userMessageService.deleteFriend(userId, friendId)){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("获取聊天记录")
    @RequestMapping(value = "/getChat", method = RequestMethod.POST)
    public Result getChat(HttpServletRequest request, @RequestParam Long friendId) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return Result.ok(userMessageService.getChat(userId, friendId));
    }
    @ApiOperation("发送消息")
    @RequestMapping(value = "/sendMessage",method=RequestMethod.POST)
    public Result sendMessage(HttpServletRequest request, @RequestParam Long friendId, @RequestParam String content) {
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        boolean search = blockRelaService.search(friendId, userId);
        if (!search) return Result.fail("您已拉黑对方或对方已将您拉黑，无法发送消息");
        boolean b = userMessageService.sendMessage(userId, friendId, content);
        if(b)
            return Result.ok();
        return Result.fail("不是好友最多发三条消息，请先添加对方为好友");
    }
    @RequestMapping(value = "/getChatList",method = RequestMethod.GET)
    public Result getChatList(HttpServletRequest request){
        String token = request.getHeader("token");
        String value = (String) redisTemplate.opsForValue().get(token);
        Long userId = JwtHelper.getUserId(value);
        return userMessageService.getChatList(userId);
    }
}
